# Maintainer: Johannes Schindelin <johannes.schindelin@gmx.de>

_realname=xpdf
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
#conflicts=("${MINGW_PACKAGE_PREFIX}-poppler")
pkgver=4.02
pkgrel=1
pkgdesc="Utilities for PDF files (mingw-w64)"
arch=('any')
license=('GPL2+')
url="https://foolabs.com/xpdf/"
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
depends=("${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpaper"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-qt5"
         "${MINGW_PACKAGE_PREFIX}-zlib")
options=('strip')
source=(https://xpdfreader-dl.s3.amazonaws.com/xpdf-${pkgver}.tar.gz{,.sig}
        001-cmake.patch
        002-xpdf-4.00-64bit.patch
        005-xpdf-4.00-permissions.patch
        006-hack-argument-list-under-win.patch)
sha256sums=('52d51dc943b9614b8da66e8662b3031a3c82dc25bfc792eac6b438aa36d549a4'
            'SKIP'
            'cb7c4fde99ba33ffc999728761c3fea4f1aebac7ef0383d5a29ab79e3b40b21e'
            '75f5d7ae4bb83d2c380dfc9f65bad78574ac3f038fdd6be73c29fc9ea817e715'
            '22e0afda66f036cea8643252762e1391ed078102dd5f76f8cf862976b4b37089'
            'c2af0cd7fde36e7ba3f3935e3fa919b65ac157fd13bcca46338b3a00d5b74673')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-cmake.patch
  patch -p1 -i ${srcdir}/002-xpdf-4.00-64bit.patch
  patch -p1 -i ${srcdir}/005-xpdf-4.00-permissions.patch
  patch -p1 -i ${srcdir}/006-hack-argument-list-under-win.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DMULTITHREADED=ON \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_VERBOSE_MAKEFILE=OFF \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" install

  cd ${srcdir}/${_realname}-${pkgver}
  install -Dm 644 xpdf-qt/xpdf-icon.svg "${pkgdir}${MINGW_PREFIX}/share/pixmaps/xpdf.svg"
  install -Dm 644 README CHANGES -t "${pkgdir}${MINGW_PREFIX}/share/doc/${pkgname}"

  # remove stuff provided by poppler
  for tool in pdfdetach pdffonts pdfimages pdfinfo pdftoppm pdftops pdftotext pdftohtml; do
    rm "${pkgdir}${MINGW_PREFIX}/bin/${tool}" "${pkgdir}${MINGW_PREFIX}/share/man/man1/${tool}.1"
  done

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING3 ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING3
}
